//
//  ReaderBookModel.h
//  ebook
//
//  Created by 吴超飞 on 16/2/26.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ReaderBook.h"
#import "ReaderGlobalModel.h"
#import "ChapterContent.h"
#import "Bookmark.h"
#import "PageData.h"

#define HALFSIZE 10  //当前页前后各缓存的页数

//翻页结果
typedef enum{
    TurnSuc,
    TurnNoMore,//前（后）面没有了
    TurnNoNet//网络失败
} TurnResult;

typedef void (^ChapterCompletion)(ReaderBook *readerBook);
typedef void (^BookTextCompletion)(ChapterContent *chaptercontent,TurnResult result);
typedef void (^BookTextPageCompletion)(ChapterContent *chaptercontent,NSArray *pageArray,TurnResult result);
typedef void (^PageDataCompletion)(NSDictionary *dict,TurnResult result);//index 初始页所在的序号

//获取章节数据内容类型：前一章、当前章、后一章
typedef enum {
    PRECHAPTERCONTENT,
    CURRENTCHAPTERCONTENT,
    NEXTCHAPTERCONTENT
} ContentType;

@interface ReaderBookModel : NSObject

@property (nonatomic,retain) ReaderBook *readerBook;

/*isMiddleIn 是否从某一张直接初始化 中间进入时直接读取该章节数据、不要取上次的历史位置
 */
-(instancetype) initWithBook:(Book*)book isMiddleIn:(BOOL)ismid;

-(void) resetChapterList;//清空章节列表，重置参数
-(void) getBookmarkList:(void(^)(NSArray *bookmarks))complete;//不是每次都去都去数据库，只有当有某页书签变化时才调用updateBookmarkList更新书签列表
-(void) updateBookmarkList;
//判断当前章节是否本地已经存在了
-(BOOL)isExistCurrentChapter;
//判断当前书中的某章节是否本地已经存在了
-(BOOL)isCurrentBookExistLocalChapter:(NSString*)chapterid;


//判断是否需要更新章节列表文件
-(BOOL)needUpdateChapterlistFile;

//获取章节列表，本地有就本地读取，没有就网络获取
-(void)getChapterList:(void(^)(NSMutableArray *list))complete;
//读取网络上的章节列表文件，并存储到本地
-(void)getNetChapterList:(void(^)(NSArray *list))complete;
//检查更新并获取获取章节列表
-(void)checkUpdateAndGetchapterlist:(void(^)(NSArray *list,BOOL needReload))complete;

//获取章节数据
-(void) getChapterContentType:(ContentType)type forceUpdate:(BOOL)update complete:(BookTextCompletion)textBlock;

-(void) getBookmarkContent:(Bookmark *)mark complete:(BookTextCompletion)textBlock;

/*以某章节的第currentPage页为中心，向两头各取数据
 *update:是否强制从网络获取
 *ispercent 标识currentPage是页码还是章内百分比
 *返回字典中包含两个信息：当前页的序号index和数组data内容
 */
-(void)pageDataCenterChapter:(Chapter *)chapter forceUpdate:(BOOL)update currentPageOrPercent:(float) current isPercent:(BOOL)ispercent complete:(void(^)(NSDictionary *dict))complete;

//获取初始数据
-(void)getInitPageDataComplete:(PageDataCompletion)textBlock;

@end
